/* System includes. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* App includes. */
#include "include/stack.h"

/** @file
 * Generic stack implementation.
 */

void *salloc(int size, void *data) {
	void *mem = malloc(size);
	if(!mem)
		return NULL;
	
	memcpy(mem, data, size);
	return mem;
}

void push(stackt *stk, void *value) {
        struct stack_n *new;
	
	if((new = malloc(sizeof *new)) == NULL)
		return;

	new->next = stk->sp;
        new->value = value;
	stk->sp = new;
}

void *pop(stackt *stk) {
	struct stack_n *popped;
	void *pv;

	/* If the next node is NULL it is the base
	 * of the stack. */
	if(stk->sp == NULL)
		return NULL;

	popped = stk->sp;
	stk->sp = popped->next;
	pv = popped->value;
	free(popped);

	return pv;
}

